import { resolve } from 'node:path';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { createRequire } from 'module';
import dts from 'vite-plugin-dts';

const require = createRequire(import.meta.url);

const pkg = require('./package.json');

export default defineConfig(() => {
  return {
    build: {
      outDir: 'dist/es',
      lib: {
        entry: resolve(__dirname, 'src/index.ts'),
        formats: ['es'],
        fileName(format, entryName) {
          if (format === 'es') {
            return `${entryName}.js`;
          } else {
            return 'bundle.js';
          }
        },
      },
      rollupOptions: {
        external: [
          ...Object.keys({ ...pkg.dependencies, ...pkg.devDependencies }),
        ],
      },
    },
    plugins: [
      vue(),
      vueJsx(),
      dts({
        entryRoot: resolve(__dirname, 'src'),
        outDir: 'dist/types',
        cleanVueFileName: true,
        include: ['./src'],
        exclude: ['./src/default/**'],
      }),
    ],
  };
});
